package com.ysy.logistics.controller.device;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ysy.common.myenum.StatusEnum;
import com.ysy.common.resp.ResultData;
import com.ysy.logistics.entities.device.Device;
import com.ysy.logistics.entities.device.DeviceLife;
import com.ysy.logistics.entities.device.dto.DeviceDto;
import com.ysy.logistics.myannotation.OperateLog;
import com.ysy.logistics.myenum.DeviceType;
import com.ysy.logistics.service.device.DeviceLifeService;
import com.ysy.logistics.service.device.DeviceService;
import com.ysy.logistics.util.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author 姚斯羽
 * @date 2025/4/16 14:37
 * @description: 设备请求控制层
 */
@Slf4j
@RestController
@RequestMapping(value = "/device",name = "设备管理")
@Tag(name = "设备管理",description = "device表的CRUD")
@CrossOrigin
public class DeviceController {

    @Resource
    private DeviceService deviceService;
    @Resource
    private DeviceLifeService deviceLifeService;

    @GetMapping("/getType")
    @Operation(summary = "获取设备类型",description = "获取设备类型")
    public ResultData getType(){
        return ResultData.success(DeviceType.values());
    }

    @PostMapping("/listByPage")
    @Operation(summary = "分页查询设备信息",description = "分页查询设备信息")
    @OperateLog("查询设备信息")
    public ResultData<Page<DeviceDto>> listByPage(@RequestBody PageInfo<DeviceDto> pageInfo){
        log.debug("分页查询设备信息："+pageInfo);
        return ResultData.success(deviceService.listByPage(pageInfo));
    }

    @PostMapping("/save")
    @Operation(summary = "保存设备信息",description = "保存设备信息")
    @OperateLog("保存设备信息")
    public ResultData save(@Validated @RequestBody Device device){
        //通过设备编号查询设备是否存在
        LambdaQueryWrapper<Device> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Device::getDeviceNum,device.getDeviceNum());
        if (deviceService.exists(lqw)){
            //设备编号已存在
            return ResultData.fail("设备编号已存在");
        }
        if (!DeviceType.isExist(device.getDeviceType())){
            return ResultData.fail("设备类型不存在");
        }
        //阈值必须有一个
        if (device.getLowerThreshold()==null && device.getUpperThreshold()==null){
            return ResultData.fail("请填写一个报警值");
        }
        device.setStatus(StatusEnum.DISABLE.getCode());
        device.setCreateTime(new Date());
        //保存
        return deviceService.save(device)? ResultData.success() : ResultData.fail("保存失败");
    }

    @PostMapping("/update")
    @Operation(summary = "修改设备信息",description = "修改设备信息")
    @OperateLog("修改设备信息")
    public ResultData update(@Validated @RequestBody Device device){
        //判断
        if ( device.getId()==null) {
            return ResultData.fail("请指定要修改的设备");
        }
        //
        //通过设备编号查询设备是否是现在的设备
        LambdaQueryWrapper<Device> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Device::getDeviceNum,device.getDeviceNum())
                .ne(Device::getId,device.getId());
        if (deviceService.exists(lqw)){
            //设备编号已被使用
            return ResultData.fail("设备编号已被使用");
        }
        //修改
        return deviceService.updateById(device)? ResultData.success() : ResultData.fail("修改失败");
    }

    @GetMapping("/changeStatus/{id}")
    @Operation(summary = "修改设备状态",description = "修改设备状态")
    @OperateLog("修改设备状态")
    public ResultData changeStatus(@PathVariable("id") String id){
        Device device = deviceService.getById(id);
        if (device == null){
            return ResultData.fail("设备不存在");
        }
        Device update = new Device();
        update.setId(id);
        if (StatusEnum.ENABLE.getCode().equals(device.getStatus())){
            update.setStatus(StatusEnum.DISABLE.getCode());
        }else {
            update.setStatus(StatusEnum.ENABLE.getCode());
        }
        return deviceService.updateById(update)? ResultData.success() :ResultData.fail("修改失败");
    }

    @GetMapping("/remove/{id}")
    @Operation(summary = "删除设备信息",description = "删除设备信息")
    @OperateLog("删除设备")
    public ResultData remove(@PathVariable("id") String id){
        //查询出设备信息
        Device device = deviceService.getById(id);
        if (device == null){
            return ResultData.fail("设备不存在");
        }
        //是否启用
        if (StatusEnum.ENABLE.getCode().equals(device.getStatus())){
            return ResultData.fail("设备启用中，不能删除");
        }
        return deviceService.removeById(id)? ResultData.success() : ResultData.fail("删除失败");
    }

    @PostMapping("/getLifeList")
    @Operation(summary = "获取设备生命周期",description = "获取设备生命周期")
    @OperateLog("查看设备记录")
    public ResultData<Page<DeviceLife>> getLifeList(@RequestBody PageInfo<DeviceLife> pageInfo){
       return ResultData.success(deviceLifeService.getListByPage(pageInfo));
    }
    @GetMapping("/clearLife/{id}")
    @Operation(summary = "清除设备生命周期",description = "清除设备生命周期")
    @OperateLog("清除设备记录")
    public ResultData clearLife(@PathVariable("id") String id){
        LambdaQueryWrapper<DeviceLife> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DeviceLife::getDeviceId,id);
        return deviceLifeService.remove(lqw)? ResultData.success() : ResultData.fail("清除失败");
    }
}
